package com.example.springtaskschedule.config;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * ExecutorShutDownHook
 * 为什么不搞这个bean,在spring容器关闭的时候调用destroy并调executor的shutdown方法？
 * 因为ThreadPoolTaskExecutor这个类，已经实现了DisposableBean，所以会自动调用shutdown
 *
 * @author zhouxianfeng
 * @date 2021-4-26
 */
//@Component
public class ExecutorShutDownHook implements DisposableBean {
    @Autowired
    @Qualifier("threadPoolTaskExecutor")
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Autowired
    @Qualifier("test1TaskExecutor")
    ThreadPoolTaskExecutor test1TaskExecutor;

    /*@Autowired
    @Qualifier("asyncExecutor")
    ThreadPoolTaskExecutor asyncExecutor;*/


    @Override
    public void destroy() throws Exception {
//        asyncExecutor.shutdown();
        test1TaskExecutor.shutdown();
        threadPoolTaskExecutor.shutdown();
    }
}
